<?php
namespace app\admin\common\model;


use think\Db;
use think\Model;

class Temp extends Model
{
    protected $pk = 'id';
    protected $table = 'qi_temp';

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    protected $dataFormat = 'Y-m-d H:i:s';


    public function getInfoById($id)
    {
        $info = Temp::where('id',$id)->find();
        return $info;
    }

    public function getPageList($pageSize = 10)
    {
        $list = Temp::order('id','desc')->paginate($pageSize);
        return $list;
    }

    /*
     * 添加模板
     */
    public function addTemp($info)
    {
        $result = Temp::create($info);
        return $result;
    }

    /*
     * 更新模板
     */
    public function editTemp($info)
    {
        $result = Temp::update($info);
        return $result;
    }

    /*
     * 删除模板
     * 先将对应的图片删除
     */
    public function delTemp($id)
    {
        $this->delTempImg($id);

        $result = Temp::where('id',$id)->delete();
        return $result;
    }

    /*
     * 删除id对应的图片
     */
    public function delTempImg($id)
    {
        $filename = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . Temp::where('id',$id)->value('temp_img');

        if (file_exists($filename)) {
            unlink($filename);
        }
    }
}